#include "pricemaintainwidget.h"
#include "ui_pricemaintainwidget.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QMessageBox>

PriceMaintainWidget::PriceMaintainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PriceMaintainWidget)
{
    ui->setupUi(this);
    initWindow();
}

PriceMaintainWidget::~PriceMaintainWidget()
{
    delete ui;
}

/**
 * @brief 初始化窗体
 */
void PriceMaintainWidget::initWindow()
{
    this->setWindowTitle("价格维护");
    ui->cb_type->addItem("物业费");
    ui->cb_type->addItem("车位费");
    ui->cb_type->addItem("维修费");

    ui->cb_unit->addItem("元");

    ui->sb_price->setMinimum(0);
    ui->sb_price->setMaximum(99999);
}

/**
 * @brief 保存
 */
void PriceMaintainWidget::on_btn_save_clicked()
{
    QString type = ui->cb_type->currentText();
    QString unit = ui->cb_unit->currentText();
    int price = ui->sb_price->value();
    QSqlQuery query;
    query.prepare("UPDATE price_manage SET price = :price, unit = :unit WHERE type = :type");
    query.bindValue(":type", type);
    query.bindValue(":price", price);
    query.bindValue(":unit", unit);
    if (!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        QMessageBox::information(this, tr("提示"), tr("保存成功"), QMessageBox::Ok);
    }
}


